package cn.youwan.market.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.EditText;

public class Utils {
	// 判断EditText是否为空
	public static boolean isNull(EditText t) {
		if (t.getText().toString() != null
				&& !t.getText().toString().trim().equals("")) {
			return false;
		}
		return true;
	}

	public static boolean hasNetwork(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	// 判断是否为wifi环境
	public static boolean isWifi(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectivityManager.getActiveNetworkInfo();
		if (info != null && info.isAvailable()) {
			if (info.getTypeName().toLowerCase().equals("wifi")) {
				return true;
			}
		}
		return false;
	}

	// 判断是否为空
	public static boolean isNull(String s) {
		if (s != null && !s.equals("")) {
			return false;
		}
		return true;
	}

	public static int getWindownHeight(Activity context) {
		Rect frame = new Rect();
		context.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		return statusBarHeight;
	}

	// md5加密
	public static String md5(String s) {
		try {

			MessageDigest digest = java.security.MessageDigest
					.getInstance("MD5");
			digest.update(s.getBytes("UTF-8"));
			byte messageDigest[] = digest.digest();

			return toHexString(messageDigest);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	private static String toHexString(byte[] bytes) {
		StringBuilder hexString = new StringBuilder();
		for (byte b : bytes) {

			int i = 0xFF & b;
			if (i < 0x10) {
				hexString.append("0" + Integer.toHexString(i));
			} else {
				hexString.append(Integer.toHexString(i));
			}
		}
		return hexString.toString();
	}

	// 判断联网方式
	public static boolean isCMWAP(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectivityManager.getActiveNetworkInfo();
		if (info == null || !info.isAvailable()) {
			return false;
		} else if (info.getTypeName().toLowerCase().equals("mobile")
				&& info.getExtraInfo().toLowerCase().equals("cmwap")) {
			return true;
		} else if (info.getTypeName().toLowerCase().equals("mobile")
				&& info.getExtraInfo().toLowerCase().equals("uniwap")) {
			return true;
		} else if (info.getTypeName().toLowerCase().equals("mobile")
				&& info.getExtraInfo().toLowerCase().equals("3gwap")) {
			return true;
		} else {
			return false;
		}
	}

	// 判断是否有SD卡
	public static boolean hasSDCard() {
		String t = android.os.Environment.getExternalStorageState();
		boolean isSD = Environment.MEDIA_MOUNTED.equals(t);
		if (isSD) {
			return getSDAvailSize();
		}
		return Environment.MEDIA_MOUNTED.equals(t);
	}

	private static boolean getSDAvailSize() {
		File path = Environment.getExternalStorageDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long AvailBlock = stat.getAvailableBlocks();
		long AvailSize = AvailBlock * blockSize;
		if (AvailSize < 8 * 1024 * 1024) {
			return false;
		}
		return true;
	}

	// 获取Imei值
	public static String getImei(Context context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String imei = tm.getDeviceId();
		return imei;
	}

	// 获取屏幕的高
	public static float getPhoneHight(Activity activity) {
		DisplayMetrics metric = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
		return metric.heightPixels;

	}

	// 获取屏幕的宽
	public static float getPhoneWidth(Activity activity) {
		DisplayMetrics metric = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
		return metric.widthPixels;

	}

	public static float getPhoneWDensity(Activity activity) {
		float f = (float) (getPhoneWidth(activity) / 320.0);
		return f;
	}

	public static float getPhoneHDensity(Activity activity) {
		float f = (float) (getPhoneHight(activity) / 480.0);
		return f;
	}

	// 获取density
	public static float getDensity(Activity activity) {
		DisplayMetrics metric = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
		float d = metric.density;
		/*
		 * if(d==2.0){ d=(float) (d*1.5); }
		 */
		return d;
	}

	public static float getTextSize(Activity activity) {
		DisplayMetrics metric = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
		float d = metric.density;
		if (d > 2.0) {
			d = (float) 2.15;
		}
		return d;
	}

	public static float getTextSize1(Activity activity) {
		DisplayMetrics metric = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
		float d = metric.density;
		if (d > 2.0) {
			d = (float) 2.04;
		}
		return d;
	}

	// 根据宽获取图片的URl
	public static String getImgUrl(Activity activity, String url) {
		int width = (int) getPhoneWidth(activity);
		if (width < 310) {
			return url + "_200.jpg";
		} else if (width < 460) {
			return url + "_260.jpg";
		} else if (width < 700) {
			return url + "_400.jpg";
		} else {
			return url + "_660.jpg";
		}
	}

	// 创建图片大小(改变图片大小)
	public static Bitmap createSizeImage(Bitmap bitmap, int newWidth,
			int newHeight) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();

		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;

		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);

		/* 将图片按照一定的比例进行缩放 */
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);
		return resizedBitmap;
	}

	public static int getEditHight(Activity activity) {
		int hight = (int) getPhoneHight(activity);
		if (hight < 500) {
			return 35;
		} else if (hight < 820) {
			return 52;
		} else if (hight < 1300) {
			return 90;
		} else {
			return (int) (getDensity(activity) * 35);
		}
	}

	public static String getAppSize(int size) {
		DecimalFormat df = new DecimalFormat("#.00");
		String str;
		float dd = (float) (size / 1024.0);
		if (dd < 1024) {
			str = df.format(dd) + "KB";
		} else {
			str = df.format(dd / 1024.0) + "MB";
		}
		return str;
	}

	// 安装应用
	public static void installApp(Context context, String pkg) {
		File file = new File(Const.APKS + Utils.md5(pkg) + ".apk");
		if (!file.exists()) {
			return;
		}
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(file);
		intent.setDataAndType(uri, "application/vnd.android.package-archive");
		context.startActivity(intent);
	}

	// 打开应用
	public static void openApp(Context context, String pkg) {
		Intent intent = new Intent();
		intent = context.getPackageManager().getLaunchIntentForPackage(pkg);
		if (intent != null) {
			context.startActivity(intent);
		} else {
			Log.d("yang", "errr: 在打开应用程序时，没有找到对应应用程序!");
		}
	}

	// 卸载应用
	public static void unstallApp(Context context, String pkg) {
		Uri uri = Uri.parse("package:" + pkg);
		Intent intent = new Intent(Intent.ACTION_DELETE, uri);
		context.startActivity(intent);
	}
}
